/**
 * �ļ����ƣ�spi.c
 * ��    �ܣ�ʵ�ֶ�ʱ����ʱ
 * ��дʱ�䣺20161007d
 * �� д �ˣ��Ƹ�
 * ע    �⣺
   �汾��:  V1.1 
*/

#include "Bsp.h"
#include "App.h"

/**
 * ?l??????spi.c
 * ??    ?????????????
 * ??????20161007d
 * ?? ? ??????
 * ?    ??
   ????:  V1.1 
*/
/* �ⲿ�궨�嶨��  --------------------------------------------------------*/
#define SPI1_PIN_RCC     RCC_APB2Periph_GPIOB
#define SPI1_RCC         RCC_APB1Periph_SPI2

#define SPI1_INTR_PIN    GPIO_Pin_1  /* HM2048_V1 */

#define SPI1_MOSI_PIN    GPIO_Pin_15
#define SPI1_MISO_PIN    GPIO_Pin_14
#define SPI1_SCK_PIN     GPIO_Pin_13 
#define SPI1_SCK_CS      GPIO_Pin_12 

#define SPI1_PROT        GPIOB 

#define SPI_CS(val)      if (val) {\
								   MODIFY_REG_BIT(SPI1_PROT, SPI1_SCK_CS, val);\
				         } else {\
								   MODIFY_REG_BIT(SPI1_PROT, SPI1_SCK_CS, val);\
						 }
						 
#define SPI_INTR(val)    if (val) {\
								   MODIFY_REG_BIT(SPI1_PROT, SPI1_INTR_PIN, val);\
				         } else {\
								   MODIFY_REG_BIT(SPI1_PROT, SPI1_INTR_PIN, val);\
						 }

#define SPI_MASTER                   SPI2
#define SPI_MASTER_CLK               RCC_APB2Periph_SPI2
#define SPI_MASTER_GPIO              GPIOB
#define SPI_MASTER_GPIO_CLK          RCC_APB2Periph_GPIOB
#define SPI_MASTER_DMA               DMA1
#define SPI_MASTER_DMA_CLK           RCC_AHBPeriph_DMA1  
#define SPI_MASTER_Rx_DMA_Channel    DMA1_Channel2
#define SPI_MASTER_Rx_DMA_FLAG       DMA1_FLAG_TC2
#define SPI_MASTER_Tx_DMA_Channel    DMA1_Channel3
#define SPI_MASTER_Tx_DMA_FLAG       DMA1_FLAG_TC3  
#define SPI_MASTER_DR_Base           0x4001300C

uint8_t spi_buf_debug[10240];
uint32_t spi_buf_debug_i = 0;

/*SPI?????'??IO*/
void SPI_GPIO_Config(void)
{ 
	GPIO_InitTypeDef GPIO_InitStructure;

	/* ????????? */
	RCC_APB2PeriphClockCmd(SPI1_PIN_RCC | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(SPI1_RCC, ENABLE);

	/* ????SCK, MOSI */
	GPIO_InitStructure.GPIO_Pin   = SPI1_SCK_CS |SPI1_SCK_PIN  |SPI1_MOSI_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;//????????
	GPIO_Init(SPI1_PROT, &GPIO_InitStructure);	
		
    /* ???????IO */
	/*
	GPIO_InitStructure.GPIO_Pin   = SPI1_INTR_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;//???????
	GPIO_Init(SPI1_PROT, &GPIO_InitStructure);

    SPI_INTR(1);	
	SPI_CS(1);
	*/
}

//?????????PCLK/2=72M/2=36M
//???????: ???????1?????'?�????36M/8=4.5MBps??bps???h??
//bps???b??bit, Bps???B???????byte
//g?: CPOL??????????? CPHA???2????????????????
//???STM32??STC??h????STC??????????????????????????????????z?NSS
//??t?????????????????g??????????�??????????????h?????????
//STM32???????????h??????????????????????????????z???????
void SPI_Mode_Init(void)
{ 
	SPI_InitTypeDef  SPI_InitStructure;
	  NVIC_InitTypeDef NVIC_InitStructure;
	
	/*????SPI?L???g?*/
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_RxOnly;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;                 //???�???g?
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS  = SPI_NSS_Hard;                    //???CS,?????????
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//72M/2/4=9MHZ
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(SPI2, &SPI_InitStructure);
	//SPI1->DR = 0x55;   
   
	//SPI?????????????
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); 
	
	NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;       
	NVIC_Init(&NVIC_InitStructure);

	SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
	
	SPI_Cmd(SPI2, ENABLE);	
}



/**
 * ????????: SPI????'??
 * ???????: ??
 * ???????: ??
 * ?????  : ??
 * ???    :		
 */
void BSP_SPI_Init(void)
{
    SPI_GPIO_Config();           
	SPI_Mode_Init();
	//SPI_Interrupt_Init();
}



void SPI2_IRQHandler(void)
{
   //if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)  
   { 
     //push data to buf	   
	 spi_buf_debug[spi_buf_debug_i] = SPI2->DR; 
	
	 if (++spi_buf_debug_i == 10000) {
		spi_buf_debug_i = 0;
	 }
	 
	 //SPI1->DR = 0x55;
	 
	 // clear flag
	 //SPI_I2S_ClearITPendingBit( SPI2 , SPI_I2S_IT_RXNE );  
   }
	 
	 //SPI_I2S_ReceiveData(SP I2);
}

